import java.io.ByteArrayOutputStream;
import java.security.MessageDigest;
import java.sql.Timestamp;

import org.junit.Test;

import utl.mofen.intf.IDBOperator;
import utl.mofen.jsonStreamHandler.HandlerFactory;
import utl.mofen.jsonStreamHandler.IJSonStreamHandler;
import utl.mofen.objects.JSonStream;
import utl.mofen.objects.KeyStream;
import utl.mofen.objects.KeyStreamCoder;
import utl.mofen.script.ScriptObjectFactory;
import utl.mofen.utils.GsonUtil;
import utl.mofen.utils.MD5Util;
import utl.mofen.utils.ResLoader;

import com.google.gson.JsonObject;
import com.logic.sysHandler.MakeCode;


public class HandlerTester {
	public final static String MD5(String s) {
		  char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
		  'a', 'b', 'c', 'd', 'e', 'f' };
		  try {
		   byte[] strTemp = s.getBytes();
		   //使用MD5创建MessageDigest对象
		   MessageDigest mdTemp = MessageDigest.getInstance("MD5");
		   mdTemp.update(strTemp);
		   byte[] md = mdTemp.digest();
		   int j = md.length;
		   char str[] = new char[j * 2];
		   int k = 0;
		   for (int i = 0; i < j; i++) {
		    byte b = md[i];
		    //System.out.println((int)b);
		    //将没个数(int)b进行双字节加密
		    str[k++] = hexDigits[b >> 4 & 0xf];
		    str[k++] = hexDigits[b & 0xf];
		   }
		   return new String(str);
		  } catch (Exception e) {return null;}
		  }
	

	@Test
	public void makeCodeTester() throws Exception
	{
	    
		IDBOperator dbOpera = ScriptObjectFactory.getDBOperator("main");
		MakeCode makeCode = new MakeCode();
		makeCode.setDBOperator(dbOpera);
		String code = makeCode.getNewCode("PO", true);
		System.out.println(code);	
		
//		Timestamp ret = new Timestamp(System.currentTimeMillis()); 
//		System.out.println(ret);

	}
	
	@Test
	public void loginTester() throws Exception
	{
		System.out.println(MD5("a"));
		System.out.println(MD5("abc"));
		System.out.println(MD5("abcdefghijklmnopqrstuvwxyz"));
		

		byte[] yhData = {35, -46, -69, 35, -70, -29, 35};
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		os.write("admin".getBytes());
		os.write(yhData);
		os.write("admin".getBytes());
		
		System.out.println(MD5Util.MD5(os.toByteArray()));
		//by = new byte[7](35, -46, -69, 35, -70, -29, 35);
		
		System.out.println(MD5Util.MD5("admin#一#恒#admin".getBytes("gbk")));

		//fe1f61fd06e6c512a1846be1e6a875ae
		String s = ResLoader.getString("/script/LoginHandler.txt");
		System.out.println(s);
		JSonStream jsonStream = new JSonStream();
		jsonStream.initialize();
		JsonObject jsonObject = GsonUtil.parseObject(s);
		jsonStream.setJsonObject(jsonObject);
		
		HandlerFactory.checkInitialize(null);
		
		IJSonStreamHandler handler = HandlerFactory.createHandler(jsonStream);
		handler.setJSonStream(jsonStream);
		handler.doAction();
		
		handler.doAction();

	}
}
